<!-- 直播编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改直播':'添加直播'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
        
      <el-form-item label="直播封面:" prop="image">
        <uploadImage :limit="1" v-model="form.image"></uploadImage>
      </el-form-item>
                    
      <el-form-item
        label="直播名称:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入直播名称"
          clearable/>
      </el-form-item>
      
      <el-form-item
        label="直播人:"
        prop="notes">
        <el-input
          :maxlength="20"
          v-model="form.notes"
          placeholder="请输入直播人"
          clearable/>
      </el-form-item>
      
      <el-form-item
        label="所属届数:"
        prop="jieshuo_id">
        <el-select
          clearable
          v-model="form.jieshuo_id"
          placeholder="请选择所属届数">
          <el-option
                  v-for="item in jieshuos"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
        </el-select>
      </el-form-item>
      
      <div v-if="isUpdate">
          <el-form-item
        label="推流地址:"
        prop="rtmp">
        <el-input
          v-model="form.rtmp"
          placeholder="请输入直播推流地址"
          clearable/>
      </el-form-item>
      
         <el-form-item
        label="播放地址:"
        prop="flv">
        <el-input
          v-model="form.flv"
          placeholder="请输入播放地址"
          clearable/>
      </el-form-item>
      
         <el-form-item
        label="录播地址:"
        prop="replay">
        <el-input
          v-model="form.replay"
          placeholder="请输入录播地址"
          clearable/>
      </el-form-item>
      </v-if></div>
                      
      <el-form-item label="上架状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">上架</el-radio>
                  <el-radio :label="2">下架</el-radio>
                </el-radio-group>
      </el-form-item>
                      
      <el-form-item label="是否禁言:">
        <el-radio-group
          v-model="form.is_jinyan">
                  <el-radio :label="1">是</el-radio>
                  <el-radio :label="2">否</el-radio>
                </el-radio-group>
      </el-form-item>
      
       <el-form-item label="直播状态:">
        <el-radio-group
          v-model="form.live_status">
                  <el-radio :label="1">即将开始</el-radio>
                  <el-radio :label="2">直播中</el-radio>
                  <el-radio :label="3">已结束</el-radio>
                </el-radio-group>
      </el-form-item>
                              
      <el-form-item label="观看人数:" prop="viewers">
        <el-input-number
          :min="0"
          v-model="form.viewers"
          placeholder="请输入观看人数"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
                              
      <el-form-item label="显示顺序:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入显示顺序"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
      
      <el-form-item label="直播时间:" prop="live_time">
            <el-date-picker
              type="datetime"
              class="ele-fluid"
              v-model="form.live_time"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择直播时间"/>
          </el-form-item>
                </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'LiveEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
           image: [
          {required: true, message: '请上传直播封面', trigger: 'blur'}
        ],
              name: [
          {required: true, message: '请输入直播名称', trigger: 'blur'}
        ],
         notes: [
          {required: true, message: '请输入直播人', trigger: 'blur'}
        ],
                  jieshuo_id: [
          {required: true, message: '请选择所属届数', trigger: 'blur'}
        ],
                  rtmp: [
          {required: true, message: '请输入直播推流地址', trigger: 'blur'}
        ],
                  flv: [
          {required: true, message: '请输入播放地址', trigger: 'blur'}
        ],
                  replay: [
          {required: true, message: '请输入录播地址', trigger: 'blur'}
        ],
                  
        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
                          
        is_jinyan: [
          {required: true, message: '请选择是否禁言', trigger: 'blur'}
        ],
                          viewers: [
          {required: true, message: '请输入观看人数', trigger: 'blur'}
        ],
                  sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
                  live_time: [
          {required: true, message: '请输入直播时间', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      jieshuos:  [],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  created() {
    this.getJieshuos();
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/live/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    
    getJieshuos(){
      this.$http
        .post("/jieshuo/index", {})
        .then(res => {
          if (res.data.code === 0) {
            this.jieshuos = res.data.data;
           
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          this.$message.error(e.message);
        });
    }
  }
}
</script>

<style scoped>
</style>
